import React from 'react';

import {Button} from 'antd';

import {get, post} from '../../../logic/api';

import ObjectList from '../../components/ObjectList';
const Operation = ObjectList.Operation;
const InputEx = ObjectList.InputEx;
const SelectEx = ObjectList.SelectEx;
const FileEx = ObjectList.FileEx;

import router from '../../../utils/routerDecorator';
import QiniuUploadimage from '../../components/QiniuUploadimage';

export default class Edit extends React.Component {

  state = {
  }

  dataIndex = [
    {
      subtitle:'分类',
      name:'cateId',
      type:'cascader',
      options:this.props.catesTree
    },{
      subtitle:'中文名称',
      name:'cname',
      type:'textarea'
    },{
      subtitle:'英文名称',
      name:'ename',
      type:'textarea'
    },{
      subtitle:'描述',
      name:'describe',
      type:'textarea'
    },{
      subtitle:'原价',
      name:'oprice'
    },{
      subtitle:'现价',
      name:'nprice'
    },{
      type:'img',
      subtitle:'头图',
      name:'image'
    },{
      subtitle:'商品图',
      name:'images3',
      type:'gentleid_publish',
      imgonly:true
    },{
      subtitle:'详情标题',
      name:'detailtitle',
      type:'textarea',
    },{
      subtitle:'详情内容',
      name:'detail',
      type:'textarea'
    },{
      subtitle:'细节描述',
      name:'particular',
      type:'textarea'
    },{
      subtitle:'注意事项',
      name:'notice',
      type:'textarea'
    }

  ];

  async componentWillMount(){
  }

  render(){
    return <Operation
      width = {800}
      text = {this.props.text?this.props.text:"编辑"}
      dataIndex = {this.dataIndex}
      record = {this.props.record}
      fetchData = {async ()=>{
        if (this.props.fetchData){
          return await this.props.fetchData(this.props.record);
        }

        var ret = await get('/admin/goods/goodsinfo/' + this.props.record.id);

        var cateId = ret.data.cateId + "";
        ret.data.cateId = [cateId];

        for(var i=0;i<this.props.catesTree.length;i++){
          for(var j=0;j<this.props.catesTree[i].children.length;j++){
            if (cateId === this.props.catesTree[i].children[j].value){
              ret.data.cateId.unshift(this.props.catesTree[i].value);
              break;
            }
          }
        }

        if (!ret.data.images3){
          ret.data.images3 = "[]";
        }

        return ret;
      }}

      onOk = {async (r, f) => {

        f.cateId = parseInt(f.cateId.pop());
        f.images3 = JSON.stringify(f.images3);
        f.oprice = f.oprice + "";
        f.nprice = f.nprice + "";

        var ret = await post('/admin/goods/goodsinfo/' + r.id, f);
        
        if (ret.ok){
          if (this.props.reload){
            this.props.reload();
          }
        }

        return ret;
      }}

      onCancel = {async ()=>{

      }}
    />
  }
}